// pages/chuantongDingdan/dingdanList/dingdanList.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabindex: '0', //分类索引
    nowpage: 1,
    sumpage: 1,
    dingdanList: [],
    moveParams: {
      scrollLeft: 0, // scroll-view滚动的距离,默认为0,因为没有触发滚动
      subLeft: 0, //点击元素距离屏幕左边的距离
      subHalfWidth: 0, //点击元素的宽度一半
      screenHalfWidth: 187.5, //屏幕宽度的一半
    },
    showflag: false
  },
  getdingdanlist() {
    let that = this
    let type
    if (that.data.tabindex == 0) {
      type = ''
    } else if (that.data.tabindex == 1) {
      type = 9
    } else if (that.data.tabindex == 2) {
      type = 1
    } else if (that.data.tabindex == 3) {
      type = 2
    } else if (that.data.tabindex == 4) {
      type = 7
    } else if (that.data.tabindex == 5) {
      type = 8
    } else if (that.data.tabindex == 6) {
      type = 4
    } else if (that.data.tabindex == 7) {
      type = 3
    }
    wx.request({
      url: app.globalData.myurl,
      data: {
        cmd: "getAgtGoodsList",
        shopId: wx.getStorageSync('shopId'),
        userId: wx.getStorageSync('userId'),
        nowPage: that.data.nowpage,
        regoodsId: that.data.regoodsId,
        type: type
      },
      header: {
        "content-type": "application/x-www-form-urlencoded",
        "token": wx.getStorageSync('token')
      },
      success(res) {
        if (res.data.zjstate == 1) {
          setTimeout(function () {
            wx.hideLoading()
            wx.stopPullDownRefresh() //停止下拉刷新
          }, 0)
          setTimeout(function () {
            wx.showModal({
              title: '您的账号已在其他设备登录，请重新登录！',
              showCancel: false, //是否显示取消按钮false去掉取消按钮
              success: function (res) {
                if (res.confirm) {
                  wx.clearStorageSync()
                  wx.reLaunch({
                    url: '/pages/login/login',
                  })
                } else {
                  wx.clearStorageSync()
                  wx.reLaunch({
                    url: '/pages/login/login',
                  })
                }
              }
            })
          }, 0)
          return
        }
        console.log(res)
        if (res.data.result == 1) {
          setTimeout(function () {
            wx.hideLoading()
            wx.stopPullDownRefresh() //停止下拉刷新
          }, 0)
          setTimeout(function () {
            wx.showToast({
              title: res.data.resultNote,
              icon: 'none',
              duration: 1500
            })
          }, 0)
        } else {
          if (that.data.nowpage == 1) {
            that.setData({
              dingdanList: res.data.orderBeans,
              sumpage: res.data.totalPage,
              showflag: true
            })
          } else {
            var list = that.data.dingdanList;
            for (var i in res.data.orderBeans) {
              list.push(res.data.orderBeans[i])
            }
            that.setData({
              dingdanList: list,
              sumpage: res.data.totalPage,
              showflag: true
            })
          }
          setTimeout(function () {
            wx.hideLoading()
            wx.stopPullDownRefresh() //停止下拉刷新
          }, 0)
        }
      },
      fail(err) {
        setTimeout(function () {
          wx.hideLoading()
          wx.stopPullDownRefresh() //停止下拉刷新
        }, 0)
        setTimeout(function () {
          wx.showToast({
            title: '网络错误，稍后重试',
            icon: 'none',
            duration: 1500
          })
        }, 0)
      }
    });
  },
  // 去评价
  toPingjia() {
    wx.navigateTo({
      url: '/pages/setting/page/pingjiaDianpu/pingjiaDianpu',
    })
  },
  // 查看物流
  toWuliu(e) {
    let dingdanId = e.currentTarget.dataset.dingdanid
    wx.navigateTo({
      url: '/pages/setting/page/wuliuXinxi/wuliuXinxi?id=' + dingdanId,
    })
  },
  // 去发货
  toFahuo(e) {
    let dingdanId = e.currentTarget.dataset.dingdanid
    let that = this
    wx.navigateTo({
      url: '/pages/order/page/qufahuo/qufahuo/qufahuo?id=' + dingdanId,
    })
    // wx.navigateTo({
    //   url: '/pages/order/page/qufahuo/daixiaopifa/daixiaopifa',
    // })
  },
  //订单详情
  toddxq(e) {
    // let type = e.currentTarget.dataset.type
    let dingdanId = e.currentTarget.dataset.dingdanid
    wx.navigateTo({
      url: '/pages/order/page/daixiaoDingdan/shenheAndfahuo/shenheAndfahuo?dingdanId=' + dingdanId,
    })
  },
  // tab滑动动画
  getRect(ele) { //获取点击元素的信息,ele为传入的id
    var that = this;
    wx.createSelectorQuery().select(ele).boundingClientRect(function (rect) {
      let moveParams = that.data.moveParams;
      moveParams.subLeft = rect.left;
      moveParams.subHalfWidth = rect.width / 2;
      that.setData({
        moveParams: moveParams
      })
      that.moveTo();
      that.getdingdanlist()
    }).exec()
  },
  //切换
  changetab: function (e) {
    let that = this
    setTimeout(function () {
      wx.showLoading({
        title: '加载中',
        mask: true,
      })
    }, 0)
    let ele = 'ele' + e.target.dataset.current
    that.setData({
      tabindex: e.target.dataset.current,
      nowpage: 1,
      dingdanList: [],
      showflag: false
    });
    that.getRect('#' + ele);
  },
  scrollMove(e) {
    let moveParams = this.data.moveParams;
    moveParams.scrollLeft = e.detail.scrollLeft;
    this.setData({
      moveParams: moveParams
    })
    console.log(e)
  },
  //计算滚动
  moveTo: function () {
    let subLeft = this.data.moveParams.subLeft;
    let screenHalfWidth = this.data.moveParams.screenHalfWidth;
    let subHalfWidth = this.data.moveParams.subHalfWidth;
    let scrollLeft = this.data.moveParams.scrollLeft;
    let distance = subLeft - screenHalfWidth + subHalfWidth;
    scrollLeft = scrollLeft + distance;
    console.log(subLeft)
    this.setData({
      scrollLeft: scrollLeft
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this
    // let ele = 'ele' + that.data.tabindex
    // that.getRect('#' + ele);
    that.setData({
      regoodsId: options.regoodsId
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this
    setTimeout(function () {
      wx.showLoading({
        title: '加载中',
        mask: true,
      })
    }, 0)
    that.setData({
      nowpage: 1,
      sumpage: 1,
      dingdanList: [],
    })
    let ele = 'ele' + that.data.tabindex
    that.getRect('#' + ele);
    // that.getdingdanlist()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    let that = this
    setTimeout(function () {
      wx.showLoading({
        title: '加载中',
        mask: true,
      })
    }, 0)
    that.setData({
      nowpage: 1,
      sumpage: 1,
      dingdanList: [],
      showflag: false
    })
    that.getdingdanlist()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let that = this
    let nowpage = that.data.nowpage
    nowpage = nowpage + 1
    let sumpage = that.data.sumpage
    if (nowpage <= sumpage) {
      setTimeout(function () {
        wx.showLoading({
          title: '加载中',
          mask: true,
        })
      }, 0)
      that.setData({
        nowpage: nowpage
      })
      that.getdingdanlist()
    } else {
      wx.showToast({
        title: '已经到底了',
        icon: 'none'
      })
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})